import java.io.*;
import java.net.*;

public class FileServer implements Runnable
{
	public void run()
	{
		ServerSocket server = null;
		Socket connect = null;
		FileOutputStream fout = null;
		try{
			server = new ServerSocket(4567);
			while (true) {
				connect = server.accept();
				DataInputStream din 
					= new DataInputStream(connect.getInputStream());
				String name = din.readUTF();
				int length = din.readInt();
				byte[] bytes = new byte[length];
				din.readFully(bytes);
				fout = new FileOutputStream(name);
				fout.write(bytes);
			}
			}catch(IOException ex){
				System.err.println(ex);
			}finally{
				try{
					connect.close();
					fout.close();
				}catch(IOException ex){
					System.err.println(ex);
				}
			}
	}

	public static void main(String[] argts)
	{
		FileServer server = new FileServer();
		Thread t = new Thread(server);
		t.start();
	}
}
